<template>
  <div>
    <el-tree
    show-checkbox
    default-expand-all
    ref="tree"
     node-key="id"
     check-strictly
    :data="list"
    :props="{ label: 'name', children: 'children' }"
    ></el-tree>

    <el-row type="flex" justify="end" height="60">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button type="info" @click="$emit('close')">取消</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from "@/api/permisson.js";
import { transDataToTreeData } from "@/utils/index";
import {getRoleDetail ,assignPerm} from '@/api/settings'
export default {
  props:{
    roleId:{
      type:String,
      required:true
    }
  },
  data() {
    return {
      list: [],

   
    };
  },
  // 页面加载渲染
  created() {
    this.loadPermissionList();
  },
  methods: {
    // 提交
  async hSubmit(){
     try {
      // 获取选中节点的id
      const permIds=this.$refs.tree.getCheckedKeys()
      const res = await assignPerm({id:this.roleId,permIds:permIds})
      console.log('assignPerm', res)
      this.$emit('close')  //关闭弹窗
      // 保存数据
    } catch (err) {
      console.log('assignPerm', err)
    }
  },
// 加载权限列表
    async loadPermissionList() {
      try {
        const res = await getPermissionList();
        console.log("getPermissionList", res);
        //   把平铺的数据转换为树形结构
        // 保存数据
        this.list = transDataToTreeData(res.data);
    const info=  await  getRoleDetail(this.roleId)
    console.log('info',info);  //info.data.permIds ===>  得到一个数组 permIds: ['604f7e17f900be1850edb154']
    this.$refs.tree.setCheckedKeys(info.data.permIds);
      } catch (err) {
        console.log("getPermissionList", err);
      }
    },
  },
};
</script>

<style>
</style>